import Vue from 'vue';
import Router from 'vue-router';
import { Util, AuthGuard } from '@/utils';
import { Environment } from '@/environments/environment';
import {userrouter} from "@/userrouter";

Vue.use(Router);

let routes =  [
        {
            path: '/base_index/:base_index?',
            name: 'base_index',
            beforeEnter: (to: any, from: any, next: any) => {
                const routerParamsName = 'base_index';
                const params: any = {};
                if (to.params && to.params[routerParamsName]) {
                    Object.assign(params, Util.formatMatrixParse(to.params[routerParamsName]));
                }
                const url: string = 'webapp/app/webapp/getappdata';
                const auth: Promise<any> = AuthGuard.getInstance().authGuard(url, params, router);
                auth.then(() => {
                    next();
                }).catch(() => {
                    next();
                });
            },
            meta: {
                caption: '应用首页',
                viewType: 'APPINDEX',
                requireAuth: true,
            },
            component: () => import('@pages/'+Environment.defaultIndexViewPath),
            children: [
                {
                    path: 'base_usergridview/:base_usergridview?',
                    name: 'base_usergridview',
                    meta: {
                        caption: '用户',
                        requireAuth: true,
                    },
                    component: () => import('@pages/base/user-grid-view/user-grid-view'),
                },
                {
                    path: 'base_accountgridview/:base_accountgridview?',
                    name: 'base_accountgridview',
                    meta: {
                        caption: '账户',
                        requireAuth: true,
                    },
                    component: () => import('@pages/base/account-grid-view/account-grid-view'),
                },
                {
                    path: 'base_usereditview/:base_usereditview?',
                    name: 'base_usereditview',
                    meta: {
                        caption: '用户',
                        requireAuth: true,
                    },
                    component: () => import('@pages/base/user-edit-view/user-edit-view'),
                },
                {
                    path: 'base_accounteditview/:base_accounteditview?',
                    name: 'base_accounteditview',
                    meta: {
                        caption: '账户',
                        requireAuth: true,
                    },
                    component: () => import('@pages/base/account-edit-view/account-edit-view'),
                },
                {
                    path: 'base_roleeditview/:base_roleeditview?',
                    name: 'base_roleeditview',
                    meta: {
                        caption: '角色',
                        requireAuth: true,
                    },
                    component: () => import('@pages/base/role-edit-view/role-edit-view'),
                },
                {
                    path: 'base_rolegridview/:base_rolegridview?',
                    name: 'base_rolegridview',
                    meta: {
                        caption: '角色',
                        requireAuth: true,
                    },
                    component: () => import('@pages/base/role-grid-view/role-grid-view'),
                },
            ]
        },
        {
            path: '/login/:login?',
            name: 'login',
            meta: {
                caption: '登录',
                viewType: 'login',
                requireAuth: false,
                ignoreAddPage: true,
            },
            beforeEnter: (to: any, from: any, next: any) => {
                router.app.$store.commit('resetRootStateData');
                next();
            },
            component: () => import('@components/'+Environment.loginViewPath),
        },
        {
            path: '*',
            redirect: 'base_index'
        },
    ];

const router = new Router({
    routes:[...userrouter, ...routes]
});

export default router;
